//
//  FRBookReadViewController+ChapterData.m
//  Fresh Reading
//
//  Created by Annie on 2020/5/25.
//  Copyright © 2020 Annie. All rights reserved.
//

#import "FRBookReadViewController+ChapterData.h"
#import "FRPagingTool.h"

@implementation FRBookReadViewController (ChapterData)








/// 章节分页
/// @param section 第几章节
/// @param fource 是否强制分页
- (BOOL)pagingChapterWithSection:(NSInteger)section fource:(BOOL)fource{
    
    FRChapterModel *cModel = self.model.chapters[section];
    BOOL res = NO;
    if (fource) {
       res = [self fr_pagingChapterWithModel:self.model.chapters[section] section:section];
    }else if (!cModel.readContents || cModel.readContents.count == 0){
       res = [self fr_pagingChapterWithModel:self.model.chapters[section] section:section];
    }
    return res;
}


- (BOOL)fr_pagingChapterWithModel:(FRChapterModel *)cModel section:(NSInteger)section{
    BOOL hasContent = cModel.contentModel && cModel.contentModel.content && cModel.contentModel.content.length > 0;
     if (!hasContent) {
         return NO;
     }
    
    CGSize size = [UIScreen mainScreen].bounds.size;
    if ([FRAdManager manager].pg_reading_banner_config.fr_showAd){    
        size = CGSizeMake(size.width, size.height - 50);
    }
    FRReadConfigModel *configModel = [FRReadConfigManager manager].configModel;
    NSMutableArray *pageAttributedStrings = [FRPagingTool pageAttributingWithModel:cModel readConfigModel:configModel maxPageSize:size edgeInsets:configModel.reading_edgeInsets];
    NSMutableArray<FRReadContentModel *> *readContents = [NSMutableArray array];
    
    
    NSInteger total = 0;
    for (NSInteger i = 0; i < pageAttributedStrings.count; i++) {
        FRReadContentModel *model = [FRReadContentModel new];
        model.fr_chapterTitle = cModel.chapterTitle;
        model.fr_bookName = self.model.name;
        model.indexPath = [NSIndexPath indexPathForRow:i inSection:section];
        model.attritedString = pageAttributedStrings[i];
        model.fr_length = model.attritedString.length;
        model.fr_offset = total;
        total += model.fr_length;
        [readContents addObject:model];
    }
    
    
    NSInteger num = 0;
    for (NSInteger i = 0; i<section; i++) {
        FRChapterModel *a_cModel = self.model.chapters[i];
        if (a_cModel.wordNum == 0) {
          num = 0;
          break;
        }
        num += a_cModel.wordNum;
    }

    NSInteger fr_pages = readContents.count;
    [readContents enumerateObjectsUsingBlock:^(FRReadContentModel * _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        model.fr_total = total;
        model.fr_pages = fr_pages;
        {
            float cper = (model.fr_offset + model.fr_length)/(model.fr_total * 1.0);
            model.fr_c_percent = cper;
            if (num == 0) {
                float befor_per = section * 1.0 / self.model.chapters.count;
                float scale = 1.0/self.model.chapters.count;
                model.fr_read_percent = befor_per + scale * cper;
            }else{
                float befor_per = num/(self.model.wordNum * 1.0);
                float scale = cModel.wordNum/(self.model.wordNum * 1.0);
                model.fr_read_percent = befor_per + scale * cper;
            }
        }
    }];
    
    if (readContents.count) {
        cModel.readContents = readContents;
        return YES;
    }else{
        
    }
    return NO;
}




@end
